package cn.abcsys.cloud.devops.runtime.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.abcsys.cloud.devops.runtime.bean.GridBean;
import cn.abcsys.cloud.devops.runtime.bean.Result;
import cn.abcsys.cloud.devops.runtime.common.Status;
import cn.abcsys.cloud.devops.runtime.dao.ImageMapper;
import cn.abcsys.cloud.devops.runtime.entity.Image;
import cn.abcsys.cloud.devops.runtime.model.ImageModel;
import cn.abcsys.cloud.devops.runtime.service.ImageService;
/**
 * 镜像管理业务处理
 * @author mayunhao
 *
 */
@Component
public class ImageServiceImpl implements ImageService {
	private static final Logger LOGGER = Logger.getLogger(ImageServiceImpl.class);
	@Autowired
	private ImageMapper imageMapper;
	@Override
	public Result insertImage(Map<String,Object> params) {
		Image image = new Image();
		try{
			String imageUuid = (String)params.get("imageUuid");
			String imageName = (String)params.get("imageName");
			int registryId = Integer.parseInt(params.get("registryId").toString());
			int projectId = Integer.parseInt(params.get("projectId").toString());
			String imageTag = (String)params.get("imageTag");
			int imageCreator =Integer.parseInt(params.get("imageCreator").toString());
			int envId =Integer.parseInt(params.get("envId").toString());
			String imageType =(String)params.get("imageType");
			byte imageScanStatus = Byte.valueOf(params.get("imageScanStatus").toString());
			image.setImageUuid(imageUuid);
			image.setImageName(imageName);
			image.setRegistryId(registryId);
			image.setImageTag(imageTag);
			image.setImageCreator(imageCreator);
			image.setImageScanStatus(imageScanStatus);
			image.setProjectId(projectId);
			image.setImageType(imageType);
			image.setEnvId(envId);
			image.setImageStatus((byte)Status.IMAGE.NORMAL.ordinal());
			List<Image> images = imageMapper.selectByImageUuid(image);
			if(images.size()>0){
				LOGGER.error("Image Exist");
				return new Result(true, "镜像已存在");
			}
		}catch(Exception e){
			LOGGER.error("请求参数转换错误："+e.getMessage());
			return new Result(false, "请求参数转换错误："+e.getMessage());
		}
		Result result = null;
		try {
			image.setDeployStatus((byte)Status.JUDGE.NO.ordinal());
			int i = imageMapper.insertImage(image);
			if(i==1){
				result = new Result(true,"创建成功");
			}else{
				result = new Result(false,"创建失败");
			}
		} catch (SQLException e) {
			LOGGER.error("创建失败:"+e.getMessage());
			result = new Result(false, "创建失败："+e.getMessage());
		}
		return result;
	}

	@Override
	public Result deleteImage(Integer imageId) {
		Result result = null;
		try {
			int i = imageMapper.deleteImage(imageId);
			if(i==1){
				result = new Result(true,"删除成功");
			}else{
				result = new Result(false,"删除失败");
			}
		} catch (SQLException e) {
			LOGGER.error("删除失败:"+e.getMessage());
			result = new Result(false, "删除失败："+e.getMessage());
		}
		return result;
	}

	@Override
	public Result updateImage(Image record) {
		Result result = null;
		try {
			int i = imageMapper.updateImage(record);
			if(i==1){
				result = new Result(true,"更新成功");
			}else{
				result = new Result(false,"更新失败");
			}
		} catch (SQLException e) {
			LOGGER.error("更新失败:"+e.getMessage());
			result = new Result(false, "更新失败："+e.getMessage());
		}
		return result;
	}

	@Override
	public Result selectByPrimaryKey(Integer imageId) {
		Result result = null;
		try {
			Image image = imageMapper.selectByPrimaryKey(imageId);
			if(null!=image){
				result = new Result(true,JSONObject.toJSON(image),"success");
			}else{
				result = new Result(false,"未查到相关结果");
			}
		} catch (SQLException e) {
			LOGGER.error("查询镜像详情失败:"+e.getMessage());
			result = new Result(false, "查询失败："+e.getMessage());
		}
		return result;
	}

	@Override
	public Result selectAllImageByEvn(Image image) {
		try {
			List<Image> images = imageMapper.selectAllImageByEvn(image);
			return new Result(true, images, "success");
		} catch (SQLException e) {
			LOGGER.error("查询镜像异常："+e.getMessage());
			return new Result(false,"查询镜像异常："+e.getMessage());
		}
	}

	@Override
	public Result deleteBatchImage(List<Integer> list) {
		Result result = null;
		try {
			int i = imageMapper.deleteBatchImage(list);
			if(i==1){
				result = new Result(true,"删除成功");
			}else{
				result = new Result(false,"删除失败");
			}
		} catch (SQLException e) {
			LOGGER.error("删除失败:"+e.getMessage());
			result = new Result(false, "删除失败："+e.getMessage());
		}
		return result;
	}

	@Override
	public GridBean getImagePageList(ImageModel imageModel) {
		int page = imageModel.getPage();
		int rows = imageModel.getRows();
		int totalpage = 0;
		Long totalNum = 0l;
		Page<Image> pageObject = new Page<Image>();
		try {
			Image image = new Image();
			image.setImageName(imageModel.getImageName());
			image.setImageStatus((byte)Status.IMAGE.NORMAL.ordinal());
			image.setRegistryId(imageModel.getRegistryId());
			image.setImageType(imageModel.getImageType());
			image.setEnvId(imageModel.getEnvId());
			image.setProjectId(imageModel.getProjectId());
			PageHelper.startPage(page, rows);
			List<Image> images = imageMapper.selectAllImageByEvn(image);
			for(Image temp: images){
				pageObject.add(temp);
			}
			totalpage = ((Page<?>) images).getPages();
			totalNum = ((Page<?>) images).getTotal();
		} catch (SQLException e) {
			LOGGER.error("分页查询集群失败:"+e.getMessage());
		}
		GridBean gridBean = new GridBean(page, totalpage, totalNum.intValue(), pageObject);
		return gridBean;
	}

	@Override
	public Result getUndeployImagesByProjectId(Integer projectId) {
		Image image = new Image();
		image.setProjectId(projectId);
		List<Image> images = imageMapper.getUndeployImagesByProjectId(image);
		return new Result(true, images, "success");
	}

}
